---
title: "Mind Of the Movement Analysis"
author: "Jonathan Pinckney, Michael Niconchuk, and Sarah Ryan"
date: "5/26/2021"
output: html_document
---

```{r setup, include=FALSE}
library(tidyverse)
library(boot)

replication.df <- read_csv("algeria_replication.csv")


# USIP Theme Colors

usip.yellow <- "#C19F53"
usip.blue <-  "#276383"
usip.dark.blue <- "#183B4B"
usip.gray <- "#949599"

# Function to Remove NAs from Mean for Bootstrapping

mean.fun <- function(data,indexes){  
  mean(data[indexes],na.rm = T)}

```

## Demographics

```{r}

summary(replication.df$Age)
table(replication.df$Gender)
table(replication.df$UrbanRural)
table(replication.df$Ethnicity)

table(replication.df$MovementParticipation_frequency)
median(replication.df$ProtestDuration,na.rm = T)

# Number of survey respondents who have not participated in Herak

sum(replication.df$MovementParticipation_frequency == "Never", na.rm = T)

```



## Motivations

Bar graph of the stated motivations for participating in the Herak

```{r}

motivation.bar.graph <- replication.df %>% 
  group_by(MovementReason) %>% 
  summarize(obs = n()) %>% 
  ggplot(aes(reorder(MovementReason,desc(obs)),obs)) + 
  geom_col(fill = usip.blue) +
  scale_x_discrete(labels = c("For The\nPrinciples","Political\nreasons","Social\nreasons",
                              "Encouragement\nfrom friends","Other","Economic\nreasons","I Don't Know","No Answer")) +
  labs(x = "",y = "") +
  theme_bw() +
  theme(axis.text = element_text(size = 18),
    axis.text.x = element_text(angle = 45,hjust = 1))

ggsave("motives_graph.png",motivation.bar.graph,width = 10,height = 6,units = "in",dpi = 320)


```


## Motivation Table

```{r}

attach(filter(replication.df,MovementParticipation_frequency_num > 1))

# Perceived low status

mean(StatusLadder_Society_Movement ,na.rm = T)
sd(StatusLadder_Society_Movement ,na.rm = T)

# Lack of agency

mean(Agency_individual,na.rm = T)
sd(Agency_individual,na.rm = T)

# Sense of victimhood

mean(Victimhood ,na.rm = T)
sd(Victimhood ,na.rm = T)

# Family Activism

table(FamilyActivism_History)

detach(filter(replication.df,MovementParticipation_frequency_num > 1))

```

### Trust in Institutions

```{r pressure, echo=FALSE}
boots.Group_Trust <- replication.df %>%
  select(Trust_Gov,Trust_Mil,Trust_AlHerOrg,Trust_AlHerMem,
         Trust_AlgMedia,Trust_otherCountries) %>% 
  pivot_longer(cols = everything(),names_to = "variable",values_to = "value") %>% 
  group_by(variable) %>% 
  nest() %>% 
  mutate(boots = map(data,function(x){boot(x$value,mean.fun,R = 1000)}),
         means = map_dbl(boots,~ .$t0) - 1, # Subtracting 1 from means so that column displays properly
         ci.low = map_dbl(boots,~ boot.ci(.)$normal[2]) - 1,
         ci.high = map_dbl(boots,~ boot.ci(.)$normal[3]) - 1
         )

trust.barplot <- ggplot(boots.Group_Trust,aes(x = reorder(variable,desc(means)),y = means)) + 
  geom_col(fill = usip.blue,width = 0.5) +
  geom_errorbar(aes(x = variable,ymin = ci.low,ymax = ci.high),width = 0.2) +
  theme_minimal() +
  labs(y = "Average Trust",x = "", caption = 'Columns are average response to question "Indicate how much you agree \n with the following statement: I trust this group" for each of the groups mentioned. \n Responses measured on a 1-7 scale from "Strongly Disagree" to "Strongly Agree." \n Errors bars are a 95% bootstrapped confidence interval.') +
  scale_x_discrete(labels = c("Herak \n Members","Herak \n Organizers","Military","Other \n Countries","Government","Algerian \n Media")) +
  scale_y_continuous(breaks = c(0:6),labels = c(1:7)) +
  theme(text = element_text(size = 18))

trust.barplot
ggsave(trust.barplot,filename = "trust_bar.jpg",width = 10,height = 6,units = "in",dpi = 320)
```

## Benefits

## Emotion Frequency

```{r}
table_Group_Feeling<- select(replication.df,Feeling_1:Feeling_10) %>% 
  pivot_longer(cols = everything()) %>% 
  group_by(value) %>% 
  summarize(count = n()) %>% 
  filter(!(is.na(value)))
  
group.feeling.plot <- table_Group_Feeling %>% 
  mutate(value = if_else(value == "Nother","Nothing",value)) %>% 
  filter(!(is.na(value))) %>% 
  ggplot(aes(reorder(value,desc(count)),count)) + 
    geom_col(fill = usip.blue) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 45,hjust = 1),
          text = element_text(size = 18)) +
    labs(x = "",y = "")

ggsave(group.feeling.plot,filename = "group_feeling.jpg",width = 12,height = 6,units = "in",dpi = 320)
```


```{r}
# In-group identification

replication.df %>% 
  filter(MovementParticipation_frequency_num > 1) %>% # Drop those who said they never participated
  select(GroupIdentification,Efficacy_AlHerak,
         SharedGrievance,Trust_AlHerMem) %>%
  rename(Efficacy = Efficacy_AlHerak,Trust = Trust_AlHerMem) %>% 
  drop_na() %>% 
  summarize(across(everything(), list(mean = mean,sd = sd,min = min,max = max))) %>% 
  pivot_longer(cols = everything()) %>% 
  separate(name,into = c("variable","stat"),sep = "_") %>% 
  pivot_wider(names_from = "stat",values_from = "value")


```

## Low Levels of Participation and Identification

How much do even those with very low levels of participation identify with the Herak?

```{r}
id.part.table <- replication.df %>% 
  mutate(MovementParticipation_frequency = fct_collapse(MovementParticipation_frequency, 
                                                        Weekly = c('every Friday','every Tuesday'),
                                                        `Several times per year` = c('Multiple times per year','Every two months'))
         )  



id.boxplot <- ggplot(filter(id.part.table,!(is.na(MovementParticipation_frequency))),aes(MovementParticipation_frequency,GroupIdentification)) + 
  geom_boxplot(fill = usip.yellow) +
  scale_x_discrete(labels = c("Never","Once or Twice\nper Year","Several Times\nper Year","Every Month","Every Two Weeks","Weekly","Multiple Times\nper Week")) +
  scale_y_continuous(limits = c(1,7)) +
  theme_bw() +
  theme(text = element_text(size = 18),
    axis.text.x = element_text(hjust = 1,angle= 45)) +
  labs(y = "Group Identification",x = "Participation Frequency")

ggsave(id.boxplot,filename = "id_boxplot.jpg",width = 10,height = 6,units = "in",dpi = 320)

```


## Differences

```{r}
# Sacred Values and Entitativity

replication.df %>% 
  filter(MovementParticipation_frequency_num > 1) %>% # Drop those who said they never participated
  select(SacredValues, Entitativity) %>%
  drop_na() %>% 
  dplyr::summarize(across(everything(), list(mean = mean,sd = sd,min = min,max = max))) %>% 
  pivot_longer(cols = everything()) %>% 
  separate(name,into = c("variable","stat"),sep = "_") %>% 
  pivot_wider(names_from = "stat",values_from = "value")


# Additional Sacred Values Analysis

participant.replication.df <- replication.df %>% 
  filter(MovementParticipation_frequency_num > 1)

sum(participant.replication.df$SacredValues == 7,na.rm = T)

hist(participant.replication.df$SacredValues)

nrow(participant.replication.df)

participant.replication.df %>% 
  filter(SacredValues_1 < 4 | SacredValues_2 < 4 | SacredValues_3 < 4) %>% 
  nrow()

```


